#include "sheetwidgetpg2.h"
#include "ui_sheetwidgetpg2.h"

// CHARACTER MODEL
#include "toccharmodel.h"

// ITEM MODEL
#include <QStandardItemModel>
#include "qvarianttablemodel.h"

SheetWidgetPg2::SheetWidgetPg2(QObject* chModel, QWidget *parent) :
    WidgetWithGdrData(parent),
    ui(new Ui::SheetWidgetPg2)
{
    ui->setupUi(this);

    if ( chModel == 0 )
        ch = new TocCharModel(this);
    else
        ch = chModel;

    clues = new QVariantTableModel();
    contacts = new QVariantTableModel();
    weapons = new QVariantTableModel();

    clues->setColumnCount(3);
    clues->setHeaderData(0, Qt::Horizontal, tr("Description"));
    clues->setHeaderData(1, Qt::Horizontal, tr("Location"));
    clues->setHeaderData(2, Qt::Horizontal, tr("Leads to"));

    contacts->setColumnCount(3);
    contacts->setHeaderData(0, Qt::Horizontal, tr("Name"));
    contacts->setHeaderData(1, Qt::Horizontal, tr("Location"));
    contacts->setHeaderData(2, Qt::Horizontal, tr("Notes"));

    weapons->setColumnCount(7);
    weapons->setHeaderData(0, Qt::Horizontal, tr("Description"));
    weapons->setHeaderData(1, Qt::Horizontal, tr("Damage"));
    weapons->setHeaderData(2, Qt::Horizontal, tr("Point Black"));
    weapons->setHeaderData(3, Qt::Horizontal, tr("Close"));
    weapons->setHeaderData(4, Qt::Horizontal, tr("Near"));
    weapons->setHeaderData(5, Qt::Horizontal, tr("Long"));
    weapons->setHeaderData(6, Qt::Horizontal, tr("Notes"));

    ui->tvClues->setModel(clues);
    ui->tvContacts->setModel(contacts);
    ui->tvWeapons->setModel(weapons);

    connect(ui->tbClues, SIGNAL(addClicked()), SLOT(appendClue()));
    connect(ui->tbContacts, SIGNAL(addClicked()), SLOT(appendContact()));
    connect(ui->tbWeapons, SIGNAL(addClicked()), SLOT(appendWeapon()));
}

SheetWidgetPg2::~SheetWidgetPg2()
{
    delete clues;
    delete contacts;
    delete weapons;
    delete ui;
}

void SheetWidgetPg2::setGdrData(QVariantMap* data)
{
    gdrData = data;
}

QObject* SheetWidgetPg2::chModel()
{
    return ch;
}

void SheetWidgetPg2::refreshFromData(QObject* chModel)
{
    ch = chModel;

    clues->importData(ch->property("Clues"));
    contacts->importData(ch->property("Contacts"));
    weapons->importData(ch->property("Weapons"));
}

void SheetWidgetPg2::updateData(QObject* chModel)
{
    if ( chModel != 0 && ch != chModel )
        ch = chModel;

    ch->setProperty("Clues",    clues->toVariantList());
    ch->setProperty("Contacts", contacts->toVariantList());
    ch->setProperty("Weapons",  weapons->toVariantList());
}

void SheetWidgetPg2::appendClue()
{
    appendClue("", "", "");
}

void SheetWidgetPg2::appendContact()
{
    appendContact("", "", "");
}

void SheetWidgetPg2::appendWeapon()
{
    appendWeapon("", "", "", "", "", "", "");
}

void SheetWidgetPg2::appendClue(QString desc, QString loc, QString leadsto)
{    
    QList<QVariant> list;
    list << desc << loc << leadsto;
    clues->appendRow(list);
}

void SheetWidgetPg2::appendContact(QString name, QString loc, QString notes)
{
    QList<QVariant> list;
    list << name << loc << notes;

    contacts->appendRow(list);
}

void SheetWidgetPg2::appendWeapon(QString desc, QString dmg, QString ptblack, QString close, QString near, QString lng, QString notes)
{
    QList<QVariant> list;
    list << desc << dmg << ptblack
         << close << near << lng << notes;

    weapons->appendRow(list);
}
